﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ElectronicsMarket.Service.IService;
using ElectronicsMarket.Service.Models;
namespace ElectronicsMarket.Service.ServiceImpl
{
    public class ReceiptImpl:IReceipt
    {
        private ElectronicsMarketDBClassesDataContext eMEntity;
        public ReceiptImpl()
        {
            eMEntity = new ElectronicsMarketDBClassesDataContext();
        }
        public void InsertReceipt(RECEIPT receipt)
        {
            try
            {
                eMEntity.RECEIPTs.InsertOnSubmit(receipt);
                eMEntity.SubmitChanges();
            }
            catch
            {
                throw new NotImplementedException();
            }
        }
        public void UpdateReceipt(RECEIPT receipt)
        {
            RECEIPT rec = eMEntity.RECEIPTs.Single(r => r.RECEIPT_ID == receipt.RECEIPT_ID);
            rec.RECEIPT_TOTAL=receipt.RECEIPT_TOTAL;

            eMEntity.SubmitChanges();
        }
        public long getLastPreceiptId()
        {
            if (eMEntity.RECEIPTs.ToList() == null)
                return 0;
            else
            {
                return eMEntity.RECEIPTs.OrderByDescending(o => o.RECEIPT_ID).Select(o => o).First().RECEIPT_ID;
            }
        }
    }
}